package com.tianxiao.faas.biz.corner.service.user;

import com.alibaba.fastjson.JSON;
import com.qcloud.cos.utils.Md5Utils;
import com.tianxiao.faas.biz.domain.entity.UserExt;
import com.tianxiao.faas.mapper.dao.UsersModelMapper;
import com.tianxiao.faas.mapper.model.UsersModel;
import com.tianxiao.faas.mapper.model.UsersModelExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UsersModelMapper usersModelMapper;

    public void insert(String mobile, String name, String passwd, String img, List<String> access) {
        UsersModel record = new UsersModel();
        record.setName(name);
        record.setMobile(mobile);
        record.setImg(img);
        String s = Md5Utils.md5Hex(passwd);
        UserExt userExt = new UserExt(s);
        if (!CollectionUtils.isEmpty(access)) {
            userExt.setAccess(access);
        }
        long created = System.currentTimeMillis();
        record.setCreated(created);
        record.setUpdated(created);
        record.setDeleted(0);
        record.setValueBox(JSON.toJSONString(userExt));
        usersModelMapper.insertSelective(record);
    }

    public UsersModel insert(String nickName, String img, String wxInfo) {
        UsersModel record = new UsersModel();
        record.setName(nickName);
        record.setNickname(nickName);
        if (img != null) {
            record.setImg(img);
        }
        UserExt userExt = new UserExt();
        if (StringUtils.isNotBlank(wxInfo)) {
            userExt.setWxInfo(wxInfo);
        }
        long created = System.currentTimeMillis();
        record.setCreated(created);
        record.setUpdated(created);
        record.setDeleted(0);
        record.setValueBox(JSON.toJSONString(userExt));
        usersModelMapper.insertSelective(record);
        return record;
    }

    public void update(UsersModel user) {
        user.setUpdated(System.currentTimeMillis());
        usersModelMapper.updateByPrimaryKey(user);
    }

    public List<UsersModel> list(List<Long> userIds) {
        UsersModelExample example = new UsersModelExample();
        example.createCriteria().andIdIn(userIds);
        List<UsersModel> usersModels = usersModelMapper.selectByExample(example);
        return usersModels;
    }

    public UsersModel getByMobile(String mobile) {
        UsersModelExample example = new UsersModelExample();
        example.createCriteria().andMobileEqualTo(mobile).andDeletedEqualTo(0);
        List<UsersModel> usersModels = usersModelMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(usersModels)) {
            return null;
        }
        return usersModels.get(0);
    }

    public UsersModel getById(Long id) {
        UsersModel usersModel = usersModelMapper.selectByPrimaryKey(id);
        return usersModel;
    }
}
